# -*- coding: UTF-8 -*-

import MySQLdb
from testdemo.com.bean.User import User

class UserDao:
    def __init__(self,sql):
        self.sql = sql;

    def query(self):
        list = [];
        # 打开数据库连接
        db = MySQLdb.connect("localhost", "root", "", "test")

        # 使用cursor()方法获取操作游标
        cursor = db.cursor()

        # SQL 查询语句
        #sql = "SELECT * FROM EMPLOYEE \
         #      WHERE INCOME > '%d'" % (1000)
        try:
            # 执行SQL语句
            cursor.execute(self.sql)
            # 获取所有记录列表
            results = cursor.fetchall()
            for row in results:
                id = row[0]
                name = row[1]
                age = row[2]
                u = User(id,name,age);
                list.append(u)
                # 打印结果
                print "id=%s,name=%s,age=%d" % \
                      (id, name, age)
        except:
            print "Error: unable to fecth data"

        # 关闭数据库连接
        db.close()
        return list